package com.etc.delivery.controller.driver;

import com.etc.delivery.dto.DriverDto;
import com.etc.delivery.entity.Driver;
import com.etc.delivery.entity.Warehouse;
import com.etc.delivery.entity.Waybill;
import com.etc.delivery.service.driver.IDriverService1;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/driver")
@Controller("Controller3")
public class DriverController1 {

    @Resource(name = "DriverServiceImpl3")
    private IDriverService1 iDriverService;


    @RequestMapping("/register")
    public String register(String name, String sex, String phone, String password, String email, Integer warehouseId, ModelMap model) {
        if (warehouseId == 0) {
            return "driver/register";
        }
        Driver driver = new Driver();
        driver.setName(name);
        driver.setSex(sex);
        driver.setPhone(phone);
        driver.setPassword(password);
        driver.setEmail(email);
        driver.setWarehouseId(warehouseId);
        List<Driver> d = iDriverService.checkPhone(phone);
        if (!d.isEmpty()) {
            return "driver/register";
        } else {
            boolean flag = iDriverService.register(driver);
            if (flag) {
                model.addAttribute("msg", "注册成功");
                return "driver/login";
            } else {
                model.addAttribute("msg", "注册失败，该账号已存在");
                return "driver/register";
            }
        }


    }

    @RequestMapping("/DriverLogin")
    public String DriverLogin(String phone, String password, HttpServletRequest request, ModelMap model) {

        List<Driver> list = iDriverService.DriverLogin(phone, password);
        HttpSession session = request.getSession();

        if (!list.isEmpty()) {
            session.setAttribute("driverId", list.get(0).getId());
            session.setAttribute("warehouseId", list.get(0).getWarehouseId());
            model.addAttribute("msg", "登录成功");
            return "driver/index";
        } else {
            model.addAttribute("msg", "登录失败，账号或密码错误");
            return "driver/login";
        }


    }

    @RequestMapping("/queryAllWaybill")
    public ModelAndView queryAllWaybill(Waybill waybill, HttpSession session) {
        session.getAttribute("warehouseId");

        List<Waybill> wlist = iDriverService.queryAllWaybill();
        ModelAndView mv = new ModelAndView();
        mv.addObject("wlist", wlist);
        mv.setViewName("driver/queryAllWaybill");

        return mv;
    }


    @RequestMapping("/queryAllWaybillByPage")
    public ModelAndView queryAllWaybillByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam
            (value = "pageSize", defaultValue = "3") Integer pageSize, HttpSession session) {
        Integer warehouseId = (Integer) session.getAttribute("warehouseId");

        PageInfo<Waybill> pageInfo = iDriverService.queryAllWaybillByPage(pageNum, pageSize, warehouseId);
//        System.out.println("开始");
//        System.out.println(pageInfo.getList());
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("driver/selectyd");

        return mv;
    }

    @RequestMapping("/queryAllWaybillByPage1")
    public ModelAndView queryAllWaybillByPage1(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam
            (value = "pageSize", defaultValue = "3") Integer pageSize, HttpSession session) {
        Integer warehouseId = (Integer) session.getAttribute("warehouseId");

        PageInfo<Waybill> pageInfo = iDriverService.queryAllWaybillByPage1(pageNum, pageSize, warehouseId);
//        System.out.println("开始");
//        System.out.println(pageInfo.getList());
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("driver/selectyd1");

        return mv;
    }


    @RequestMapping("/modifyPassword")
    public String modifyPassword(@RequestParam("phone") String phone, @RequestParam("password") String password, @RequestParam("newpassword") String reNewPwd, @RequestParam("reNewPwd") String newpassword, ModelMap model) {
        if (newpassword.equals(reNewPwd)) {
            List<Driver> list = iDriverService.DriverLogin(phone, password);
            if (list.isEmpty()) {
                model.addAttribute("msg", "账号密码有误！");
                return "driver/xgmm";
            }
            boolean flag = iDriverService.modifyPassword(phone, newpassword);
            if (flag) {
                model.addAttribute("msg", "修改密码成功！");
                return "driver/login";
            } else {
                model.addAttribute("msg", "修改密码失败！");
                return "driver/xgmm";
            }

        } else {
            model.addAttribute("msg", "两次输入密码不一致！");
            return "driver/xgmm";
        }
    }

    @RequestMapping("/queryDriverByPhone")
    public Driver queryDriverByPhone(String phone) {
        Driver driver = this.iDriverService.queryDriverByPhone(phone);
        return driver;
    }

    @RequestMapping("/modifyDriver")
    @ResponseBody
    public Map<String, String> modifyDriver(String name, String sex, String email, HttpSession session) {
        System.out.println(sex);
        Integer id = (Integer) session.getAttribute("driverId");
        Driver driver = new Driver();
        driver.setId(id);
        driver.setName(name);
        driver.setSex(sex);
        driver.setEmail(email);
        boolean flag = iDriverService.modifyDriver(driver);
        Map<String, String> map = new HashMap<>();

        if (flag) {
            map.put("code", "1");
            map.put("msg", "修改成功");
        } else {
            map.put("code", "0");
            map.put("msg", "修改失败");
        }
        return map;
    }

    @RequestMapping("/modifyWaybillStatus")
    @ResponseBody
    public String modifyWaybillStatus(String waybillNo, HttpSession session) {
        Integer driverId = (Integer) session.getAttribute("driverId");
        Integer warehouseId = (Integer) session.getAttribute("warehouseId");
        boolean flag = iDriverService.modifyWaybillStatus(waybillNo, driverId, warehouseId);
        if (flag) {
            return "1";
        } else {
            return "0";
        }
    }

    @RequestMapping("/modifyWaybillStatus1")
    @ResponseBody
    public String modifyWaybillStatus1(String waybillNo, HttpSession session) {
        Integer driverId = (Integer) session.getAttribute("driverId");
        boolean flag = iDriverService.modifyWaybillStatus1(waybillNo, driverId);
        if (flag) {
            return "1";
        } else {
            return "0";
        }
    }


    @RequestMapping("/queryWaybillByDriverId")
    public ModelAndView queryWaybillByDriverId(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam
            (value = "pageSize", defaultValue = "3") Integer pageSize, HttpSession session) {
        Integer id = (Integer) session.getAttribute("driverId");
        PageInfo<Waybill> pageInfo = iDriverService.queryWaybillByDriverId(pageNum, pageSize, id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("driver/wdyd");

        return mv;
    }

    @RequestMapping("/queryWaybillByStatus")
    public ModelAndView queryWaybillByStatusByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam
            (value = "pageSize", defaultValue = "3") Integer pageSize) {

        PageInfo<Waybill> pageInfo = iDriverService.queryWaybillStatusByPage(pageNum, pageSize);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("driver/selectyd");

        return mv;
    }

    @RequestMapping("/queryWaybillByStatus1")
    public ModelAndView queryWaybillByStatus1ByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam
            (value = "pageSize", defaultValue = "3") Integer pageSize) {

        PageInfo<Waybill> pageInfo = iDriverService.queryWaybillStatus1ByPage(pageNum, pageSize);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("driver/selectyd");

        return mv;
    }

    @RequestMapping(value = "queryMyInfo")
    @ResponseBody
    public List<DriverDto> queryMyInfo(HttpSession session) {
        Integer id = (Integer) session.getAttribute("driverId");


        return iDriverService.queryMyInfo(id);
    }

    @RequestMapping(value = "arrived")
    @ResponseBody
    public Map<String, String> arrived(String waybillNo) {

        boolean flag = iDriverService.arrived(waybillNo);

        Map<String, String> map = new HashMap<>();

        if (flag) {
            map.put("code", "1");
            map.put("msg", "运单确认成功送达");
        } else {
            map.put("code", "0");
            map.put("msg", "操作失败");
        }
        return map;


    }

    @RequestMapping(value = "queryWareName")
    @ResponseBody
    public List<Warehouse> queryWareName() {
        return iDriverService.queryWareName();
    }


    @RequestMapping("loginOut")
    public String loginOut(HttpSession session) {
        session.invalidate();
        return "driver/login";
    }

    @RequestMapping(value = "getPassword")
    @ResponseBody
    public String getPassword(String yanzheng, String phone, String password, HttpSession session) {
        String flag1 = "0";
        boolean flag = false;
        String yy = (String) session.getAttribute("Captcha");
        System.out.println(yanzheng);
        System.out.println(yy);
        if (yy.equals(yanzheng)) {
            flag = iDriverService.modifyPassword(phone, password);
            if (flag) {
                return "1";
            } else {
                return "2";
            }
        } else {
            return flag1;
        }


    }


    @RequestMapping(value = "checkEmail")
    @ResponseBody
    public boolean checkEmail(String email, String phone) {

        boolean flag = iDriverService.checkEmail(email, phone);

        return flag;
    }


}
